/*
 *  $Id: HolyJudgement.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

namespace Ragima.Cards001.Light.Spells {
    public class HolyJudgement: SpellCard {
        public HolyJudgement(): base("001_l_s_holy_judgement") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Light;
            Value = 100;
            AddManaCost(ElementType.Light, 3);
            AddCondition(new DelegateCondition(GetConditionPhrase(), HasNotHolyCreatures));
        }

        public override void DoAction(TargetArgs args) {
            List<Creature> list = GetAllNotHolyCreatures(args.Player);
            if (list.Count == 0) return;
            int health = 0;
            foreach (Creature cr in list) health += cr.Health;
            health /= list.Count;
            foreach (Creature cr in list) cr.SetHealth(health);
        }

        private static bool HasNotHolyCreatures(AbstractObject caster, Card card) {
            List<Creature> list = GetAllNotHolyCreatures(caster.Owner);
            return list.Count > 0;
        }

        private static List<Creature> GetAllNotHolyCreatures(Player owner) {
            List<Creature> list = new List<Creature>();
            GetAllNotHolyCreatures(owner, list);
            GetAllNotHolyCreatures(owner.Enemy, list);
            return list;
        }
        private static void GetAllNotHolyCreatures(Player owner, List<Creature> list) {
            foreach (Creature cr in owner.GetCreatures())
                if (!cr.IsHoly)
                    list.Add(cr);
        }
    }
}